
# 基类
import time

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait

class Base():

    # 初始化驱动
    def __init__(self,driver):
        self.driver=driver
    # 打开网址
    def open_url(self,url):
        self.driver.get(url)

    def find_elelment(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)


    # 点击
    def click(self,loc):
        self.find_elelment(loc).click()
    # 输入
    def send_keys(self,loc,text):
        self.find_elelment(loc).send_keys(text)

    # 切换窗口
    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    # 滚动任意像素
    def scrollTo_x(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    # Ctrl事件
    def ctrl_div(self,loc,x):
        self.find_elelment(loc).send_keys(Keys.CONTROL,f'{x}')

    def move_to_ele(self,loc):
        ActionChains(self.driver).move_to_element(self.find_elelment(loc)).perform()